package com.et.machine.manager.vm.internet

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.Html
import android.text.Spanned
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import com.et.machine.R
import com.et.machine.convert.machineInfo
import com.et.machine.databinding.FragmentInternetBinding
import com.et.machine.databinding.ItemMsgListBinding
import com.et.machine.util.Adapter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.util.*

@Suppress("DEPRECATION")
class InternetFragment : Fragment() {

    companion object {
        const val BAI_DU = "https://www.baidu.com/"
        const val QQ = "http://www.qq.com/"
        const val A_LI_YUN = "https://www.aliyun.com/"
    }

    private lateinit var viewModel: InternetViewModel
    private lateinit var binding: FragmentInternetBinding
    private lateinit var adapter: Adapter<Spanned, ItemMsgListBinding>
    private var msgList: MutableList<Spanned> = mutableListOf()

    private val timer = Timer()
    private var task: TimerTask = object : TimerTask() {
        override fun run() {
            sendRequest(BAI_DU, binding.baidu)
            sendRequest(QQ, binding.tengxun)
            sendRequest(A_LI_YUN, binding.aliyun)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View = FragmentInternetBinding.inflate(inflater, container, false).apply {
        binding = this
    }.root

    @SuppressLint("SetTextI18n")
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProvider(this).get(InternetViewModel::class.java)

        initBar()

        timer.schedule(task, 0, 60 * 1000)

        binding.again.setOnClickListener {
            sendRequest(BAI_DU, binding.baidu)
            sendRequest(QQ, binding.tengxun)
            sendRequest(A_LI_YUN, binding.aliyun)
        }

        adapter = Adapter({ view, _, msg ->
            view.logMsg.text = msg
        }, { inflater, parent ->
            ItemMsgListBinding.inflate(inflater, parent, false)
        }, msgList)
        binding.recyclerview.also {
            it.adapter = adapter
            it.layoutManager = LinearLayoutManager(requireContext()).apply {
                orientation = LinearLayoutManager.VERTICAL
            }
        }

    }

    @SuppressLint("SetTextI18n")
    private fun initBar() {
        // back
        binding.layoutTitle.back.setOnClickListener {
            Navigation.findNavController(it).popBackStack()
        }

        binding.layoutTitle.uuid.text =
            "机器编号：${machineInfo?.uuid}"

        binding.layoutTitle.title.setText(R.string.internet_label)
    }

    @SuppressLint("SetTextI18n")
    fun sendRequest(url: String, view: TextView) {
        GlobalScope.launch(Dispatchers.Main) {
            viewModel.getWorkStatus(url).apply {
                if (msgList.size > 100) msgList.clear()
                msgList.add(Html.fromHtml(content))
                adapter.notifyDataSetChanged()
                binding.recyclerview.scrollToPosition(msgList.size - 1)
                if (time != 0) {
                    view.text = "$time ms"
                }
            }
        }
    }

    override fun onPause() {
        super.onPause()
        timer.cancel()
    }


}